#include "AppDelegate.h"
#import <HyphenateChat/HyphenateChat.h>



@interface AppDelegate()<UNUserNotificationCenterDelegate>



@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GeneratedPluginRegistrant registerWithRegistry:self];
    // Override point for customization after application launch.
   // [self diposeFultterMethodFunction];
    dispatch_after(5, dispatch_get_main_queue(), ^{
        [self registerPush];
    });
  
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}



//注册远程通知
-(void)registerPush{
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    //清除角标
    UIApplication *application = [UIApplication sharedApplication];
    application.applicationIconBadgeNumber = 0;
    center.delegate = self;
    // 小角标、声音、弹窗
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
        if (!error) {
            if(granted){
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication] registerForRemoteNotifications];
                });
              
            }
            NSLog(@"request authorization succeeded!");
        }
    }];
    
   
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    // 异步方法

        [EMClient.sharedClient registerForRemoteNotificationsWithDeviceToken:deviceToken completion:^(EMError *aError) {
              if (aError) {
                  NSLog(@"bind deviceToken error: %@", aError.errorDescription);
              }else{
                  NSLog(@"绑定成功");
              }
          }];

}
/**
 2.1注册APNs失败
 */
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"注册APNS失败:%@",error);
}

//  iOS 10: App在前台获取到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    
    NSLog(@"willPresentNotification：%@", notification.request.content.userInfo);
    // 处理 推送数据
    NSDictionary *contentDic=notification.request.content.userInfo;
    // 根据APP需要，判断是否要提示用户Badge、Sound、Alert
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
    [[EMLocalNotificationManager sharedManager] userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
}
//  iOS 10: 点击通知进入App时触发
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
//    NSLog(@"didReceiveNotification：%@", response.notification.request.content.userInfo);
//    // 处理 推送数据
//    NSDictionary *contentDic=response.notification.request.content.userInfo;
//    completionHandler();
    [[EMLocalNotificationManager sharedManager] userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//    if (gMainController) {
//        [gMainController didReceiveLocalNotification:notification];
//    }
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//    if (gMainController) {
//        [gMainController jumpToChatList];
//    }
   [[EMClient sharedClient] application:application didReceiveRemoteNotification:userInfo];
}



#pragma mark ========== 处理Flutter相关的调用方法 =============

-(void)diposeFultterMethodFunction
{
    FlutterViewController* flutterViewController = (FlutterViewController *)[self topViewController];

}

//获取当前最上面的ViewController
- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:self.window.rootViewController];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
}







@synthesize description;

@end
